import io.restassured.RestAssured;
import io.restassured.config.SSLConfig;
import io.restassured.http.ContentType;
import io.restassured.response.Response;

import java.util.HashMap;

import static io.restassured.RestAssured.*;

public class RestAssuredHelper {

    private static String JSESSIONID="";

    public static void setJSESSIONID(String jsessionid){
        JSESSIONID = jsessionid;
    }

    //测试get请求
    public static Response getTest(String url,HashMap<String,Object> params) {
        return given().log().all()
                .cookie("JSESSIONID",JSESSIONID)
                // 配置SSL 让所有请求支持所有的主机名
                .config((RestAssured.config().sslConfig(new SSLConfig().relaxedHTTPSValidation())))
                .params(params)
        .when()
                .get(url)
        .then()
                .log().all()
        .extract()
                .response();
    }

    //测试POST请求
    public static Response postTest(String url,HashMap<String,Object> formParams,HashMap<String,Object> queryParams,HashMap<String,Object> pathParams) {
        Response response=
        given().
                sessionId(JSESSIONID).
                contentType("application/json").  // 设置JSON格式
                body(formParams).
                queryParams(queryParams).
                pathParams(pathParams).
                urlEncodingEnabled(false).
                log().all().
        when().//触发条件
                post(url).
        then().
                log().all().
        extract().
                response();
        return response;
    }

    //测试PUT请求
    public static Response putTest(String url,HashMap<String,Object> formParams,HashMap<String,Object> queryParams,HashMap<String,Object> pathParams) {
        return given().log().all().
                cookie("JSESSIONID",JSESSIONID).
                contentType(ContentType.JSON).  // 设置JSON格式
                formParams(formParams).
                queryParams(queryParams).
                pathParams(pathParams).
        when().//触发条件
                put(url).
        then().
                log().all().
        extract().
                response();
    }

    //测试DELETE请求
    public static Response deleteTest(String url,HashMap<String,Object> pathParams) {
        return given().log().all().
                cookie("JSESSIONID",JSESSIONID).
                contentType(ContentType.JSON).  // 设置JSON格式
                pathParams(pathParams).
        when().//触发条件
                delete(url).
        then().
                log().all().
        extract().
                response();
    }

}
